/**
 * 登录
 */
function login() {
    $("#login").bind("click",function () {
        var param = {};
        //验证码登录
        if($("#passwordLoginUL").css("display") == "none"){
            var phone = $("#phone").val();
            var smsCode = $("#smsCode").val();
            var channel = $("#channel").val();
            if(phone == "" || phone == null || phone == undefined){
                showErrorMsg("手机号不能为空");
                return;
            }
            if(smsCode == "" || smsCode == null || smsCode == undefined){
                showErrorMsg("验证码不能为空");
                return;
            }

            param = {mobile:phone,smscode:smsCode,type:"0",channel:channel};
            $.ajax({
                type:"POST",
                url:"/loginMagazine",
                data:param,
                success:function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    if(data.code == "0000"){
                        window.location.href = "welcomeMagazine?mobile="+phone+"&channel="+data.channel;
                    }else {
                        showErrorMsg(data.msg);
                    }

                },
                error:function (jqXHR, textStatus, errorThrown) {
                    showErrorMsg("系统异常");
                }
            })
        }else {

            var account = $("#account").val();
            var password = $("#password").val();
            var channel = $("#channel").val();
            if(account == "" || account == null || account == undefined){
                showErrorMsg("手机号不能为空");
                return;
            }
            if(password == "" || password == null || password == undefined){
                showErrorMsg("密码不能为空");
                return;
            }
            param = {mobile:account,password:password,type:"1",channel:channel};
            $.ajax({
                type:"POST",
                url:"/loginMagazine",
                data:param,
                success:function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    if(data.code == "0000"){
                        window.location.href = "welcomeMagazine?mobile="+account+"&channel="+data.channel;
                    }else {
                        showErrorMsg(data.msg);
                    }

                },
                error:function (jqXHR, textStatus, errorThrown) {
                    showErrorMsg("系统异常");
                }
            })
        }


    });

}



/**
 * 切换登录方式
 */
function switchLogin() {
    $("#switchLogin").bind("click",function () {
       if($("#passwordLoginUL").css("display") == "none"){
           $("#passwordLoginUL").css("display","block");
           $("#smsLoginUL").css("display","none");
           $("#switchLogin").text("验证码登录");
       }else {
           $("#passwordLoginUL").css("display","none");
           $("#smsLoginUL").css("display","block");
           $("#switchLogin").text("账号密码登录");
       }
    });
}

/**
 *获取短信验证码
 */
function getSmsCode() {
    $("#yz").bind("click",function () {
        var phone = $("#phone").val();
        if(phone == "" || phone == null || phone == undefined){
            showErrorMsg("手机号不能为空");
            return;
        }else {
            $.ajax({
                type:"POST",
                url:"/sendSmsCode",
                data:{mobile:phone},
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.code == "0000"){
                        smsCodeDJS();
                    }else {
                        showErrorMsg(data.msg);
                    }


                },
                error:function (jqXHR, textStatus, errorThrown) {
                    showErrorMsg("系统异常");
                }
            })
        }

    })
}

/**
 * 验证码倒计时
 */
function smsCodeDJS() {
    $("#djs").css("display","block");
    $("#yz").css("display","none");
    console.log("sss222");
    var startTime = 60;
    djsFlag =setInterval(function () {
        console.log("sss333");
        startTime--;
        $("#djs").text(startTime+"S");
        if(startTime == 0){
            console.log("sss444");
            startTime = 60;
            clearInterval(djsFlag);
            $("#djs").text("60S");
            $("#djs").css("display","none");
            $("#yz").css("display","block");
        }
    },1000)
}

/**
 * sub-toast显示错误信息
 * @param msg
 */
function showErrorMsg(msg) {
    $("#sub-toast span").html(msg);
    $("#sub-toast").css("display","block");
    setTimeout(function () {
        $("#sub-toast").css("display","none");
        $("#sub-toast span").html("");
    },3000)
}

/**
 * 订阅
 * id产品id
 * ischarge 0免费，1收费
 */
function subMaganize(id,ischarge,status,istocancel) {

    console.log("id:"+id+",ischarge:" + ischarge+",status:" + status + ",istocancel:" + istocancel);

    $("#popProductId").val(id);
    if(ischarge == 0){
        $("#popIsCharge").val("0");
    }else {
        $("#popIsCharge").val("1");
    }
    popToast(status,istocancel);
}

/**
 * 弹出框显示
 */
function popToast(status,istocancel) {
    if(istocancel == 1){
        mui.toast("您本月仍可使用，请下月再订购");
    }else {
        if(status == 1){
            showPopContext("是否退订该杂志");
        }else {
            showPopContext("是否订购该杂志");
        }

        $("#zhezhao").css("display","block");
        $("#popUpBox").css("display","block");
    }


}

/**
 * 取消订阅监听
 */
function cancelListener() {
    $("#cancel").bind("click",function () {
        $("#zhezhao").css("display","none");
        $("#popUpBox").css("display","none");

        $("#popProductId").val("");
        $("#popIsCharge").val("");
    });
}

/**
 * 确认订购
 */
function confirmListener() {
    $("#confirm").bind("click",function () {

        var flag = $("#popContext").text();
        console.log("flag:"+flag);

        var productId = $("#popProductId").val();
        var isCharge = $("#popIsCharge").val();

        if(isCharge == "0"){

            if(flag == "是否退订该杂志"){
                unSubFreeProduct(productId);
            }else {
                orderFreeProduct(productId);
            }

        }else {
            if(flag == "是否退订该杂志"){
                unSubH5Produc(productId);
            }else {
                orderH5Product(productId);
            }

        }
    });
}

/**
 * 退订免费产品
 * @param productId
 */
function unSubFreeProduct(productId) {

    var phone = $("#mobile").val();
    var channel = $("#channel").val();
    var showType = $("#showType").val();

    $.ajax({
        type:"POST",
        url:"/unSubFreeMagazine",
        data:{mobile:phone,isCharge:0,productId:productId},
        success:function (data) {
            data = $.parseJSON(data);
            console.log("data:" + data);
            if(data.code == "0000"){
                window.location.href = "welcomeMagazine?mobile="+phone+"&channel="+channel;
            }else if(data.code == "1006"){
                window.location.href = "login?channel="+channel;
            }else {
                //关闭确认弹出框和遮罩
                $("#popUpBox").css("display","none");
                $("#zhezhao").css("display","none");
                mui.toast("系统繁忙，请稍后再试");
            }


        },
        error:function (jqXHR, textStatus, errorThrown) {
            showErrorMsg("系统异常");
        }
    })

}

/**
 * 退订h5产品
 * @param productId
 */
function unSubH5Produc(productId) {
    var phone = $("#mobile").val();
    var channel = $("#channel").val();
    var showType = $("#showType").val();

    $.ajax({
        type:"POST",
        url:"/unSubH5Magazine",
        data:{mobile:phone,isCharge:1,productId:productId},
        success:function (data) {
            data = $.parseJSON(data);
            console.log("data:" + data);
            if(data.code == "0000"){
                //关闭确认弹出框和遮罩
                $("#popUpBox").css("display","none");
                $("#zhezhao").css("display","none");
                mui.toast("处理中，退订成功将收到短信通知");
             //   window.location.href = "welcomeMagazine?mobile="+phone+"&channel="+channel;
            }else if(data.code == "1006"){
                window.location.href = "login?channel="+channel;
            }else {
                //关闭确认弹出框和遮罩
                $("#popUpBox").css("display","none");
                $("#zhezhao").css("display","none");
                mui.toast("系统繁忙，请稍后再试");
            }


        },
        error:function (jqXHR, textStatus, errorThrown) {
            showErrorMsg("系统异常");
        }
    })
}

/**
 * 订购免费产品
 */
function orderFreeProduct(productId) {
    var phone = $("#mobile").val();
    var channel = $("#channel").val();
    var showType = $("#showType").val();

    $.ajax({
        type:"POST",
        url:"/subFreeMagazine",
        data:{mobile:phone,isCharge:0,productId:productId},
        success:function (data) {
            data = $.parseJSON(data);
            console.log("data:" + data);
            if(data.code == "0000"){
                window.location.href = "welcomeMagazine?mobile="+phone+"&channel="+channel;
            }else if(data.code == "1006"){
                window.location.href = "login?channel="+channel;
            }else {
                //关闭确认弹出康
                $("#popUpBox").css("display","none");
                $("#zhezhao").css("display","none");
                mui.toast(data.msg);
            }


        },
        error:function (jqXHR, textStatus, errorThrown) {
            showErrorMsg("系统异常");
        }
    })
}
/**
 * 订购h5产品
 * @param productId
 */
function orderH5Product(productId) {

    var phone = $("#mobile").val();
    var channel = $("#channel").val();
    var showType = $("#showType").val();

    $.getJSON("/subH5Magazine",{mobile:phone,isCharge:1,productId:productId,channel:channel},function (datas) {
        /*console.log("datas:" + datas);
        datas = $.parseJSON(datas);*/
        if(datas.code == "0000"){
            //关闭确认弹出康
            $("#popUpBox").css("display","none");

            var channelCode = datas.channelCode;
            var cpId = datas.cpId;
            var cpOrderId = datas.cpOrderId;
            var goodsCode = datas.goodsCode;
            var mobile = datas.mobile;
            var orderType = datas.orderType;
            var timestamp = datas.timestamp;
            var sign = datas.sign;
            var callBackUrl = datas.callBackUrl;
            var timeOut = datas.timeOut;

            var params = {
                "channelCode":channelCode,// 林科渠道
                "cpId":cpId,//内容提供商号
                "cpOrderId":cpOrderId,//订单号
                "goodsCode":goodsCode,//产品id
                "mobile":mobile,//手机号
                "orderType":orderType,//计费类型 0 点播
                "timestamp": timestamp//时间戳 10位

            }

            params.sign = sign;
            params.callBackUrl = callBackUrl;//接收消息的页面地址


            var $dom = $('<iframe id="uniauthframe" width="90%" height="70%" frameborder="no" border="0"　 src="https://zz.mail.wo.cn:29004/api/view/unicomSms.jsp"></iframe>')
            $dom.on('load', function() {
                $('#uniauthframe')[0].contentWindow.postMessage(JSON.stringify(params), "https://zz.mail.wo.cn:29004/api/view/unicomSms.jsp")

            })
            $('#changeH5window').append($dom);

            var data="";
            window.addEventListener('message',function(e) {//监听

                data = e.data;
                var resultCode = data.resultCode;
                var resultMsg = data.resultMsg;

                setTimeout(function () {
                    if (data == "") {
                        $("#zhezhao").css("display","none");
                        $("#changeH5window").css("display", "none");
                        mui.toast("系统繁忙,请稍后重试!");
                    }
                }, timeOut);

                if(resultCode==9000){
                    $("#zhezhao").css("display","block");
                    $("#changeH5window").css("display","block");
                }else  if(resultCode==9001){
                    //H5页面 点击取消按钮 刷新变更套餐页面
                    $("#changeH5window").css("display", "none");
                    window.location.href = "welcomeMagazine?mobile="+phone+"&channel="+channel;
                }else if(resultCode==0){

                    $("#zhezhao").css("display","none");
                    $("#changeH5window").css("display","none");

                    mui.toast("处理中，最终订购结果以短信通知为准");


                }else {//H5认证 点击返回码不是0的，都返回套餐变更失败
                    $("#zhezhao").css("display","none");
                    $("#changeH5window").css("display","none");
                    mui.toast("订阅失败");
                }
                params.resultCode=resultCode;
                params.resultMsg=resultMsg;
                codeRecord(params);
            }, false);


        }else if(datas.code == "1006"){
            window.location.href = "login?channel="+channel;
        }else if(datas.code == "1008"){
            //关闭确认弹出康
            $("#popUpBox").css("display","none");
            $("#zhezhao").css("display","none");
            mui.toast(datas.msg);
        }else {
            //关闭确认弹出康
            $("#popUpBox").css("display","none");
            $("#zhezhao").css("display","none");
            mui.toast(datas.msg);

        }

    });
}

/**
 * 记录h5监听日志
 * @param params
 */
function codeRecord(params) {
    $.post("listenH5Log", params, function () {
    });
}

/**
 * 弹出框显示内容
 * @param msg
 */
function showPopContext(msg) {
    $("#popContext").text(msg);
}

$(document).ready(function () {
    login();
    switchLogin();
    getSmsCode();
    cancelListener();
    confirmListener();
});